package com.rxyl.love.petdoctor.acticity;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.fragment.CircleFragment;
import com.rxyl.love.petdoctor.fragment.DoctorFragment;
import com.rxyl.love.petdoctor.fragment.HospitalFragment;
import com.rxyl.love.petdoctor.fragment.MainFragment;
import com.rxyl.love.petdoctor.service.LocationService;
import com.rxyl.love.petdoctor.util.SharedUtil;
import com.rxyl.love.petdoctor.view.SelectItemsView;


@ContentView(R.layout.activity_main)
public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    @ViewInject(R.id.rg_main)
    private RadioGroup radioGroup;

    private SlidingMenu mSlidingMenu;
    private FragmentManager mManager;
    private FragmentTransaction mTransaction;
    private LocationBroadcaseRec locationBroadcaseRec;
    private Intent toService;
    private MainFragment mainFragment;
    private DoctorFragment doctorFragment;
    private HospitalFragment hospitalFragment;
    private CircleFragment circleFragment;

    private SelectItemsView menu_setting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        initView();
        initSlidingMenu();
        //定位
        initLocation();
    }


    private void initView() {
        mainFragment = new MainFragment();
        doctorFragment = new DoctorFragment();
        hospitalFragment = new HospitalFragment();
        circleFragment = new CircleFragment();

        mManager = getSupportFragmentManager();
        radioGroup.setOnCheckedChangeListener(this);

    RadioButton rb = (RadioButton) radioGroup.getChildAt(0);
    rb.performClick();
}

    /**
     * 侧边栏初始化
     */
    private void initSlidingMenu() {
        mSlidingMenu = new SlidingMenu(this);

        mSlidingMenu.setMode(SlidingMenu.LEFT);

        mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);

        mSlidingMenu.setShadowWidthRes(R.dimen.shadow_width);

        mSlidingMenu.setShadowDrawable(R.drawable.shape_shadow);

        mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);

        mSlidingMenu.setFadeDegree(0.5F);

        mSlidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);

        mSlidingMenu.setMenu(R.layout.menu_page);

        menu_setting = (SelectItemsView) mSlidingMenu.findViewById(R.id.menu_setting);
        menu_setting.setOnClickListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        mTransaction = mManager.beginTransaction();
        switch (checkedId) {
            //首页
            case R.id.rb_main:
                mTransaction.replace(R.id.container_main, mainFragment);
                break;
            //找医生
            case R.id.rb_doctor:
                mTransaction.replace(R.id.container_main, doctorFragment);
                break;
            //找医院
            case R.id.rb_hospital:
                mTransaction.replace(R.id.container_main, hospitalFragment);
                break;
            //圈子
            case R.id.rb_circle:
                mTransaction.replace(R.id.container_main, circleFragment);
                break;
        }
        mTransaction.commit();
    }

    public void showSlingMenu() {
        mSlidingMenu.toggle();
    }

    public void loadMoreHospital() {
        RadioButton rb = (RadioButton) radioGroup.getChildAt(2);
        rb.performClick();
    }

    // 定位相关
    private void initLocation() {
        //动态注册广播
        locationBroadcaseRec = new LocationBroadcaseRec();
        IntentFilter filter = new IntentFilter();
        filter.addAction("mainMsg");
        registerReceiver(locationBroadcaseRec, filter);
        //开始定位服务
        toService = new Intent(MainActivity.this, LocationService.class);
        startService(toService);
    }

    //广播接受定位
    class LocationBroadcaseRec extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            final String cityName = intent.getStringExtra("cityName");
            final String latitude  = intent.getStringExtra("latitude");
            final String longitude = intent.getStringExtra("longitude");
            if (cityName.contains(SharedUtil.getString("cityname"))) {
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("您当前定位的城市为" + cityName)
                    .setMessage("请选择是否切换城市？");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mainFragment.setCityName(cityName,latitude ,longitude );

                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            builder.show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.menu_setting:
                //设置页面
                startActivity(new Intent(this,SettingActivity.class));
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
      //  unregisterReceiver(locationBroadcaseRec);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(toService);
        unregisterReceiver(locationBroadcaseRec);
    }
}
